package denoise;

public class BeliefNode
{
    private int myX, myY;
    private int myCurValue;

    private int [] myMessageFromAbove, myMessageFromBelow, myMessageFromLeft, myMessageFromRight, myMessageFromBoss;
    private int [] myCurVector;

    public static BeliefNode [][] createArrayOfNodes(int width, int height)
    {
        BeliefNode [][] nodes = new BeliefNode[width][height];
        
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                nodes[i][j].initialize(i, j);
            }
        }

        return nodes;
    }

    public void initialize(int x, int y)
    {
        myX = x;
        myY = y;
        myCurValue = 0;
        myCurVector = new int [2];
        for (int i = 0; i < 2; i++) {
            myCurVector[i] = 0;
            myMessageFromAbove[i] = 0;
            myMessageFromBelow[i] = 0;
            myMessageFromLeft[i] = 0;
            myMessageFromRight[i] = 0;
            myMessageFromBoss[i] = 0;
        }
    }

    public void setCurValue(int value)
    {
        myCurValue = value;
        myCurVector = new int [2];
        for (int i = 0; i < 2; i++) {
            myCurVector[i] = 0;
        }
    }
    
    public int getCurValue()
    {
        return myCurValue;
    }

    public void setMessage(int x, int y, int [] message)
    {
        // Above
        if (myX > x && myY == y) {
            myMessageFromAbove = new int[2];
            for (int i = 0; i < 2; i++) {
                myMessageFromAbove[i] = message[i];
            }
        }

        //  Below
        if (myX < x && myY == y) {
            myMessageFromBelow = new int[2];
            for (int i = 0; i < 2; i++) {
                myMessageFromBelow[i] = message[i];
            }
        }

        // Left
        if (myX == x && myY > y) {
            myMessageFromLeft = new int[2];
            for (int i = 0; i < 2; i++) {
                myMessageFromLeft[i] = message[i];
            }
        }

        // Right
        if (myX == x && myY < y) {
            myMessageFromRight = new int[2];
            for (int i = 0; i < 2; i++) {
                myMessageFromRight[i] = message[i];
            }
        }

        // Boss
        if (myX == x && myY == y) {
            myMessageFromBoss = new int[2];
            for (int i = 0; i < 2; i++) {
                myMessageFromBoss[i] = message[i];
            }
        }
    }

    public void computeCurrentValue()
    {
        for (int i = 0; i < 2; i++) {
            myCurVector[i] += myMessageFromAbove[i] + 
                              myMessageFromBelow[i] + 
                              myMessageFromLeft[i] + 
                              myMessageFromRight[i] + 
                              myMessageFromBoss[i];
        }
        
        int posValue = myCurVector[0];
        int negValue = myCurVector[1];
        
        myCurValue = (posValue < negValue) ? posValue : negValue;
    }
    
    public int [] getMessageForNodeBoss()
    {
        int [] message = new int[2];

        message[0] = myCurValue + 1;
        message[1] = myCurValue - 1;

        for (int i = 0; i < 2; i++) {
            message[i] += myCurVector[i];
            message[i] -= myMessageFromBoss[i];
        }

        return message;
    }

    public int [] getMessageForNodeAbove()
    {
        int [] message = new int[2];

        message[0] = myCurValue + 1;
        message[1] = myCurValue - 1;

        for (int i = 0; i < 2; i++) {
            message[i] += myCurVector[i];
            message[i] -= myMessageFromAbove[i];
        }

        return message;
    }

    public int [] getMessageForNodeBelow()
    {
        int [] message = new int[2];

        message[0] = myCurValue + 1;
        message[1] = myCurValue - 1;

        for (int i = 0; i < 2; i++) {
            message[i] += myCurVector[i];
            message[i] -= myMessageFromBelow[i];
        }

        return message;
    }

    public int [] getMessageForNodeLeft()
    {
        int [] message = new int[2];

        message[0] = myCurValue + 1;
        message[1] = myCurValue - 1;

        for (int i = 0; i < 2; i++) {
            message[i] += myCurVector[i];
            message[i] -= myMessageFromLeft[i];
        }

        return message;
    }

    public int [] getMessageForNodeRight()
    {
        int [] message = new int[2];

        message[0] = myCurValue + 1;
        message[1] = myCurValue - 1;

        for (int i = 0; i < 2; i++) {
            message[i] += myCurVector[i];
            message[i] -= myMessageFromRight[i];
        }

        return message;
    }

}

